package com.xucy.springboot.common;

/**
 * @Author xucy
 * @Date 2019-03-26 11:01
 * @Description 自创数组包装类
 **/

public class MyArray {

    private long[] arr;
    private int size;

    MyArray() {
        arr = new long[50];
    }

    MyArray(int size) {
        arr = new long[size];
    }

    /**
     * 插入数据
     *
     * @param
     * @return
     * @Author xucy
     * @Date 11:03 2019-03-26
     */
    public void insert(long value) {
        int arrLength = arr.length;
        if (size >= arrLength) {
            throw new RuntimeException("数组已满，不能插入数据");
        }

        arr[size] = value;
        size++;
    }

    /**
     * 插入顺序数据
     *
     * @param
     * @return
     * @Author xucy
     * @Date 11:03 2019-03-26
     */
    public void insertOrder(long value) {
        int arrLength = arr.length;
        if (size >= arrLength) {
            throw new RuntimeException("数组已满，不能插入数据");
        }
        int i;
        for (i = 0; i < size; i++) {
            if (arr[i] >= value) {
                break;
            }
        }

        for (int j = size; j > i; j--) {
            arr[j] = arr[j - 1];
        }

        arr[i] = value;
        size++;
    }

    /**
     * 二分法查找数据
     *
     * @param
     * @return
     * @Author xucy
     * @Date 14:56 2019-03-26
     */
    public int averageSearch(long value) {
        int low = 0;
        int middle = 0;
        int hign = size;

        while (true) {
            middle = (low + hign) / 2;
            if (arr[middle] == value) {
                return middle;
            } else if (hign < low) {
                return -1;
            } else {
                if (arr[middle] > value) {
                    hign = middle - 1;
                } else {
                    low = middle + 1;
                }
            }
        }
    }

    /**
     * 展示数据
     *
     * @param
     * @return
     * @Author xucy
     * @Date 11:08 2019-03-26
     */
    public void display() {
        System.out.print("[  ");
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + "  ");
        }
        System.out.print("]");
    }

    /**
     * 查找数据
     *
     * @param
     * @return
     * @Author xucy
     * @Date 11:15 2019-03-26
     */
    public int search(long value) {
        int i;
        for (i = 0; i < size; i++) {
            if (arr[i] == value) {
                break;
            }
        }

        if (i >= size) {
            return -1;
        }
        return i;
    }

    /**
     * 根据角标获取当前数据
     *
     * @param
     * @return
     * @Author xucy
     * @Date 11:21 2019-03-26
     */
    public long searchByIndex(int index) {
        isOutSize(index);

        return arr[index];
    }


    /**
     * 删除数据
     *
     * @param
     * @return
     * @Author xucy
     * @Date 11:14 2019-03-26
     */
    public int remove(int index) {
        isOutSize(index);
        int i;
        for (i = index; i < size - 1; i++) {
            arr[i] = arr[i + 1];
        }
        size--;
        return i;
    }

    /**
     * 判断角标是否越界
     *
     * @param
     * @return
     * @Author xucy
     * @Date 11:22 2019-03-26
     */
    public void isOutSize(int index) {
        if (index < 0 || index >= arr.length) {
            throw new ArrayIndexOutOfBoundsException();
        }
    }


    public static void main(String[] args) {
        MyArray myArray = new MyArray(10);
        myArray.insert(23);
        myArray.insert(24);
        myArray.insert(25);
        myArray.insert(26);
        myArray.insert(27);

        myArray.display();

        System.out.println("\n" + myArray.search(26));
        System.out.println(myArray.searchByIndex(2));

        myArray.remove(1);
        myArray.display();

        MyArray myArray1 = new MyArray(10);
        myArray1.insertOrder(23);
        myArray1.insertOrder(12);
        myArray1.insertOrder(13);
        myArray1.insertOrder(18);
        myArray1.insertOrder(15);
        myArray1.insertOrder(10);
        myArray1.insertOrder(9);
        myArray1.insertOrder(21);
        myArray1.display();

        System.out.println(myArray1.averageSearch(9));
    }
}
